<template>
  <div class="app-container">
    <div class="filter-container">
      <!-- 用户名订单号查询有误 -->
      <el-select
        v-model="orderReqDto.id"
        filterable
        clearable
        :filter-method="dataFilter"
        placeholder="选择渠道"
        style="width: 150px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      >
        <el-option v-for="item in options" :key="item.channelId" :label="item.channel_name" :value="item.channelId" />
      </el-select>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>
    </div>
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="tableData"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="渠道编号" align="center" width="80">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="渠道名" align="center" width="180">
        <template slot-scope="{row}">
          <span>{{ row.nickname }}</span>
        </template>
      </el-table-column>
      <el-table-column label="签名" align="center" min-width="100">
        <template slot-scope="{row}">
          <span>{{ row.sign }}</span>
        </template>
      </el-table-column>
      <el-table-column label="秘钥" align="center" min-width="200">
        <template slot-scope="{row}">
          <span>{{ row.merKey }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="orderReqDto.current"
      :limit.sync="orderReqDto.size"
      @pagination="buyQuantityList"
    />
  </div>
</template>

<script>
import { buyQuantityList } from '@/api/order'
import { getChannelIdAndNick } from '@/api/user'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
export default {
  name: 'Orderlist',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      tableData: [],
      times: [],
      total: 0,
      tableKey: 0,
      orderReqDto: {
        size: 10,
        current: 1,
        startTime: '',
        endTime: '',
        phone: '',
        username: '',
        orderNo: ''
      },
      countOrder: {},
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '订单流水'
      },
      dialogPvVisible: false,
      pvData: [],
      rules: {
        type: [{ required: true, message: 'type is required', trigger: 'change' }],
        timestamp: [{ type: 'date', required: true, message: 'timestamp is required', trigger: 'change' }],
        username: [{ required: true, message: 'title is required', trigger: 'blur' }]
      },
      listLoading: false,
      orderNoForDetail: '',
      optionsCopy: [],
      options: [],
      priceoptionsCopy: [],
      priceoptions: [],
      value: ''
    }
  },
  created() {
    this.buyQuantityList(this.orderReqDto)
    this.getChannelIdAndNick()
  },
  methods: {
    dataFilter(val) {
      this.value = val
      if (val) { // val存在
        this.options = this.optionsCopy.filter((item) => {
          if (!!~item.label.indexOf(val) || !!~item.label.toUpperCase().indexOf(val.toUpperCase())) {
            return true
          }
        })
      } else { // val为空时，还原数组
        this.options = this.optionsCopy
      }
    },
    async getChannelIdAndNick() {
      const res = await getChannelIdAndNick(this.orderReqDto)
      res.data.data.forEach(element => {
        if (element.label !== null) {
          this.optionsCopy.push(element)
          this.options.push(element)
        }
      })
    },
    pricedataFilter(val) {
      this.value = val
      if (val) { // val存在
        this.priceoptions = this.priceoptionsCopy.filter((item) => {
          console.log(item)
          if (!!~item.channel_name.indexOf(val) || !!~item.channel_name.toUpperCase().indexOf(val.toUpperCase())) {
            return true
          }
        })
      } else { // val为空时，还原数组
        this.priceoptions = this.priceoptionsCopy
      }
    },

    // 获取订单列表
    async buyQuantityList(orderReqDto) {
      const res = await buyQuantityList(this.orderReqDto)
      if (res.data.data.records === null) {
        this.tableData = null
      } else {
        this.tableData = res.data.data.records
        this.total = res.data.data.total
      }
    },

    handleFilter() {
      this.orderReqDto.current = 1
      if (this.times == null) {
        this.orderReqDto.endTime = ''
        this.orderReqDto.startTime = ''
      } else if (this.times.length === 0) {
        this.orderReqDto.endTime = ''
        this.orderReqDto.startTime = ''
      } else {
        this.orderReqDto.endTime = this.times[1]
        this.orderReqDto.startTime = this.times[0]
      }
      this.buyQuantityList(this.orderReqDto)
    },

    handleDelete(row, index) {
      this.$notify({
        title: 'Success',
        message: 'Delete Successfully',
        type: 'success',
        duration: 2000
      })
      this.list.splice(index, 1)
    }
  }
}
</script>

<style scoped>

</style>
